package com.czxy.Demo26;

public class MyTask implements Runnable {
    private int num = 100;

    @Override
    public void run() {
        while (true){
            if (sale() == 0){
                break;
            }
        }
    }

    public synchronized int sale(){
        if (num > 0){
            System.out.println(Thread.currentThread().getName()+"号窗口卖了一张票，现在剩余"+num+"张票");
            num--;
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }else{
            return 0;
        }
    }
}
